import router from "@/router"
import NProgress from "nprogress"
import "nprogress/nprogress.css"
NProgress.configure({ showSpinner: false })
// const session = localStorage.getItem("session");
//免登陆白名单
const whiteList = ["/login", "/404"]

function permission(to, next) {
  if (sessionStorage.getItem("role")) {
    // 已登录
    if (to.path === "/login") {
      next()
      NProgress.done()
    } else {
      to.matched.length === 0 ? next("/404") : next()
    }
  } else {
    // 未登录
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next()
    } else {
      next(`/login?redirect=${to.fullPath}`) // 未登录重定向到登录页
      NProgress.done()
    }
  }
}

router.beforeEach(async (to, from, next) => {
  NProgress.start()
  await permission(to, next)
})

router.afterEach(() => {
  NProgress.done()
})
